<template>
  <el-form :model="ruleForm" ref="ruleForm" label-width="100px">
    <el-form-item label="选择类型">
      <el-select v-model="ruleForm.pak_type" placeholder="请选择类型" @change="typeChange">
        <el-option label="原生zip文件" value="1"></el-option>
        <el-option label="H5zip文件" value="2"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="版本号">
      <el-input v-model="ruleForm.version" readonly></el-input>
    </el-form-item>
    <el-form-item label="下载地址">
      <el-input v-model="ruleForm.download_addr" readonly></el-input>
    </el-form-item>
     <el-form-item label="强制更新状态">
      <el-select v-model="ruleForm.is_force_update" placeholder="请选择类型">
        <el-option label="关闭" value="0"></el-option>
        <el-option label="打开" value="1"></el-option>
      </el-select>
    </el-form-item>
     <el-form-item label="开放更新状态" prop="pak_type">
      <el-select v-model="ruleForm.is_open_update" placeholder="请选择类型">
        <el-option label="关闭" value="0"></el-option>
        <el-option label="打开" value="1"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label-width="100px">
      <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
    </el-form-item>
  </el-form>

</template>

<script>
import { responseFormat } from '../common/dataFormat';
export default {
    data() {
      return {
        ruleForm: {
          pak_type: '',
          version: '',
          download_addr: '',
          is_force_update: '',
          is_open_update: ''
        }
      };
    },
    methods: {
      typeChange(value) {
          this.$http.get('/api/PROJECT0/get_latest_appdownload_url', {
          params:{
            type: value
          }
        }).then(res => {
          console.log(res.data)
          const data = responseFormat(res.data.table);
            this.ruleForm = Object.assign(this.ruleForm, ...data);
        })
      },
      submitForm(formName) {
        this.$http.get('/api/PROJECT0/update_latest_open', {
          params: {
            type: this.ruleForm.pak_type,
            version: this.ruleForm.version,
            force_update_type: this.ruleForm.is_force_update,
            open_type: this.ruleForm.is_open_update
          }
        }).then(res => {
          if (res.data.sys_status >= 200 && res.data.sys_status < 400) {
            this.$message({
              message: '保存成功',
              type: 'success'
            })
          } else {
            this.$message({
              message: '保存失败',
              type: 'success'
            })
          }
        }, err => {
          this.$message({
              message: err,
              type: 'success'
            })
        }).catch(err => {
          this.$message({
              message: err,
              type: 'success'
            })
        })
      }
    }
  }
</script>

<style>
  .checkBtn {
    margin-left: 10px;
  }
</style>
